// app.js
const defaultTime = {
  defaultWorkTime: 25,
  defaultRestTime: 5
}

App({
  // 生命周期回调——监听小程序初始化。
  onLaunch() {
    // 获取头像昵称
    var name = wx.getStorageSync('name');
    var avatar = wx.getStorageSync('avatar');
    if (!name || !avatar) {
      // wx.getUserInfo 获取用户信息 必须是在用户已经授权的情况下调用
      wx.getUserInfo({
        success: function (res) {
          console.log(res);
          var userInfo = res.userInfo;
          wx.setStorageSync('name', userInfo.nickName)
          wx.setStorageSync('avatar', userInfo.avatarUrl)
        }
      })
    }



    // 展示本地存储能力
    // const logs = wx.getStorageSync('logs') || []
    // logs.unshift(Date.now())
    // wx.setStorageSync('logs', logs)



    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        // console.log(res);
      }
    })
    // 番茄钟
    let workTime = wx.getStorageSync('workTime')
    let restTime = wx.getStorageSync('restTime')
    if (!workTime) {
      wx.setStorage({
        key: 'workTime',
        data: defaultTime.defaultWorkTime
      })
    }
    if (!restTime) {
      wx.setStorage({
        key: 'restTime',
        data: defaultTime.defaultRestTime
      })
    }

  },
  globalData: {
    userInfo: null
  },
  writeHistory: function (todo, action, timestamp) {
    var history = wx.getStorageSync('history') || [];
    history.push({
      todo: todo ? {
        content: todo.content || '',
        tags: todo.tags || [],
        extra: todo.extra || ''
      } : null,
      action: action,
      timestamp: timestamp
    });
    wx.setStorageSync('history', history);
  }
})